package websocket

import (
	"encoding/json"
	"log"
	"tuiwen-go/internal/api"
	"tuiwen-go/internal/db"
)

// 查询项目角色
func taskSetSpeakerHandler(c *Client, data json.RawMessage) {
	log.Printf("Client %s: Handling Audio Task, Data: %s", c.ID, string(data))

	var taskData struct {
		ProjectID string `json:"project_id"`
		SpeakerID int    `json:"speaker_id"`
	} // Assuming the data contains project_id and project_name

	if err := json.Unmarshal(data, &taskData); err != nil {
		log.Printf("Client %s: 请求参数格式化失败, err: %v", c.ID, err)
		sendMessage(c, api.WxJSONResponse{Code: -1, Msg: "请求参数格式化失败"})
		return
	}

	// 更新project speak_id 字段
	_, err := db.DB.Exec("update project set speaker_id = ? where id = ?", taskData.SpeakerID, taskData.ProjectID)
	if err != nil {
		log.Printf("Client %s: 更新项目讲述人失败, err: %v", c.ID, err)
		sendMessage(c, api.WxJSONResponse{Code: -1, Msg: "更新项目讲述人失败"})
		return
	}
	sendMessage(c, api.WxJSONResponse{Code: 1, Msg: "更新项目讲述人成功"})
}
